<template>
  <div class="mod-config">
    <div class="search-form search_form" v-loading='loadingPage'>

      <el-form :model="searchForm" size="mini" label-position="right" class="demo-form-inline" label-width="110px"
               style="width: 100%;">
        <el-row :gutter="25">
          <el-col :span="4">
            <el-form-item label="考核部门" prop="deptId">
              <el-select v-model="searchForm.deptId" placeholder="请选择" filterable clearable
              >
                <el-option
                  v-for="(item ,index) in  deptList"
                  :key="index"
                  :label="item.deptName"
                  :value="item.deptId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="被考核部门" prop="bdeptId">
              <el-select v-model="searchForm.bdeptId" placeholder="请选择" filterable clearable
              >
                <el-option
                  v-for="(it ,index) in deptBList"
                  :key="index"
                  :label="it.name"
                  :value="it.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="考核类别：">
              <el-select v-model="searchForm.assetTypeId" placeholder="请选择" filterable clearable
              >
                <el-option
                  v-for="(ite ,index) in  assetTypeList"
                  :key="index"
                  :label="ite.name"
                  :value="ite.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="指标名称：">
              <el-input v-model="searchForm.keywords" placeholder="请输入指标名称" style="width: 200px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="月份：" prop="createTime">
              <el-date-picker
                v-model="searchForm.createTime"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择月">
              </el-date-picker>
            </el-form-item>
          </el-col>


          <el-col :span="4" style="text-align:center;">
            <el-button size="mini" type="primary" @click="searchBtn">查询</el-button>
            <el-button size="mini" @click="emptyClick">清空</el-button>
          </el-col>
        </el-row>
      </el-form>

    </div>
    <div>
      <el-col :span="8" style="text-align:left;margin-bottom: 10px;">
        <el-button v-hasPermi="['/depperformanceinfo/deptchart']" size="mini" type="success" @click="exportForm">导出
        </el-button>

      </el-col>
    </div>
    <el-table
      :data="tableData"
      border
      v-loading="loading"
      @selection-change="handleSelectionChange"
      @sort-change="getOrder"
      :row-key="bindRowKeys"
      style="width: 100%;">

      <el-table-column
        prop="bdeptName"
        header-align="center"
        align="center"
        label="被考核部门名称"
        width="130">
      </el-table-column>
      <el-table-column
        prop="deptName"
        header-align="center"
        align="center"
        label="考核部门名称"
        width="130">
      </el-table-column>
      <el-table-column
        prop="assetType"
        header-align="center"
        align="center"
        label="考核类别"
      >
      </el-table-column>
      <el-table-column
        prop="indicatorTaskName"
        header-align="center"
        align="center"
        label="指标名称">
      </el-table-column>
      <el-table-column
        prop="complete"
        header-align="center"
        align="center"
        label="完成情况">
      </el-table-column>
      <el-table-column
        prop="assessRuleScore"
        header-align="center"
        align="center"
        label="考核规则得分">
      </el-table-column>

      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
        width="150">
        <template slot-scope="scope">
          {{ new Date(scope.row.createTime).toLocaleDateString().replaceAll('/', '-') }}
        </template>
      </el-table-column>
    </el-table>
    <div class="progress_lok_page">
      <page
        :hidden="tableData && tableData.length === 0"
        :total="total"
        :page.sync="page"
        :limit.sync="pageSize"
        @pagination="handlePageChange"/>
    </div>
  </div>

  <!-- 弹窗, 新增 / 修改 -->
</template>

<script>
  import axios from 'axios'
  import { Encrypt } from '../../utils/cryptoAES'
  import { AesDecrypt } from '../../utils/AesDecrypt'

  export default {
    data() {
      return {
        tableData: [],
        fileList: [],
        checkedArr: [],//要导出的数据数组
        loadingPage: true,
        loading: true,
        currentPage: 0,
        page: 1,
        pageSize: 10,
        total: 0,
        searchForm: {
          keywords: '',//搜索条件
          ids: [],//导出数据集合 空为全部
          deptId: null,
          bdeptId: null,
          assetTypeId: null,
          submitStatus: null,
          createTime: null
        },
        deptList: [],
        deptBList: [],
        assetTypeList: []
      }
    },
    created() {
      // 被考核部门
      this.getDeptBList ()
      this.getDeptList ()
      // 获得考核类别
      this.getAssetTypeList ()
      this.getList ()
    },

    methods: {
      // 查询
      searchBtn() {
        this.loading = true
        this.page = 1
        this.getList ()
      },

      // 清空
      emptyClick() {
        this.searchForm.keywords = ''
        this.searchForm.deptId = ''
        this.searchForm.bdeptId = ''
        this.searchForm.assetTypeId = ''
      },
      // 获取列表
      getList() {
        var uu = {
          page: this.page,
          pageSize: this.pageSize,
          keywords: this.searchForm.keywords,
          assetDeptId: this.searchForm.deptId,
          deptId: this.searchForm.bdeptId,
          assetTypeId: this.searchForm.assetTypeId,
          createTime: this.searchForm.createTime
        }
        var jsonString = JSON.stringify (uu)
        var yy = Encrypt (jsonString)
        var encodedString = encodeURIComponent (yy)
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/deptperformofficedetail/list',
          data: encodedString
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.tableData = data.content
            this.total = data.total
            this.loading = false
            this.loadingPage = false
          } else {
            this.loading = false
            this.loadingPage = false
            this.$message.error (data.msg)
            this.tableData = []
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
// 导出
      async exportForm() {
        this.loadingPage = true
        axios ({
          method: 'post',
          responseType: 'blob',
          url: '/hr-xc-human-resources-perform-manage/generator/deptperformofficedetail/export',
          data: {
            indicatorTaskName: this.searchForm.keywords,
            assetDeptId: this.searchForm.deptId,
            deptId: this.searchForm.bdeptId,
            assetTypeId: this.searchForm.assetTypeId,
            createTime: this.searchForm.createTime

          }
        }).then (({ data }) => {
          if (data.size) {
            const downloadElement = document.createElement ('a')
            const href = window.URL.createObjectURL (data) // 创建下载的链接
            downloadElement.href = href
            downloadElement.download = decodeURI ('部门绩效考核.xls') // 下载后文件名
            document.body.appendChild (downloadElement)
            downloadElement.click () // 点击下载
            document.body.removeChild (downloadElement) // 下载完成移除元素
            window.URL.revokeObjectURL (href) // 释放掉blob对象
            this.loadingPage = false
            this.$message.success ('导出成功')
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      /** 点击分页触发的事件 */
      handlePageChange(pageInfo) {
        this.loading = true
        this.page = pageInfo.page
        this.pageSize = pageInfo.limit
        this.getList ()
      },
      getAssetTypeList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/depperformance/queryAllDeptCategory'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.assetTypeList = data
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      // 查询被考核部门
      getDeptBList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/torgdept/queryAllDeptIdAndName'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            var data = AesDecrypt (data.result)
            this.deptBList = data
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      // 查询部门
      getDeptList() {
        axios ({
          method: 'post',
          url: '/hr-xc-human-resources-perform-manage/generator/deptexaminelrules/queryAllAssetDept'
        }).then (({ data }) => {
          if (data && data.code === '00000') {
            this.deptList = data.result
          }
        }).catch (error => {
          this.$message.error ('服务器访问异常，请从新登录')
        })
      },
      // 多选
      handleSelectionChange(value) {
        this.checkedArr = value
        // element表格多选第一页选中往后翻一页全选，再回到第一页会再次选中第一页的数据，所以需要手动去重
        let hash = {}
        this.checkedArr = this.checkedArr.reduce ((preVal, curVal) => {
          hash[curVal.id] ? '' : hash[curVal.id] = true && preVal.push (curVal)
          return preVal
        }, [])
        // console.log(this.checkedArr,'this.checkedArr')
      },
      getOrder(param) {
        this.loading = true
        if (param.order === 'ascending') {
          this.searchForm.sort = 'asc'
          this.searchForm.sortField = param.prop
        } else if (param.order === 'descending') {
          this.searchForm.sort = 'desc'
          this.searchForm.sortField = param.prop
        } else {
          this.searchForm.sort = ''
          this.searchForm.sortField = ''
        }
        this.getList ()
      },
      bindRowKeys(row) {
        return row.id
      }
    }
  }
</script>
